package edu.towson.cis.cosc490.cero1.lab3;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ToDoListActivity extends Activity implements OnClickListener, OnLongClickListener{

	Button m_addButton;
	LinearLayout m_itemListView;
	int i; //variable to control switching colors
	ArrayList<ToDoItem> array = new ArrayList<ToDoItem>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_todo_list);
        initLayout();
        m_addButton.setOnClickListener(this);
        i=0;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_todo_list, menu);
        return true;
    }
    
    //initializes items to layout
    public void initLayout(){
    	m_itemListView = (LinearLayout) this.findViewById(R.id.toDoListView);
    	m_addButton = (Button) this.findViewById(R.id.startActivity);
    }

    //when add button is clicked, launch next activity for result
	public void onClick(View v) {
		Intent callActivity = new Intent(this, ToDoListActivity2.class); //calls activity to get message and priority
		int resultCode = 0;
		startActivityForResult(callActivity, resultCode);
		
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent data){
		//grabs result 0 and checks if suceeded
		if (requestCode == 0){
			if(resultCode == RESULT_OK){
				//handle result if result good
				ToDoItem item = (ToDoItem) data.getSerializableExtra("item_str");   //creates new item object passed from activity2
				array.add(item); //adds item to arrayList
				populateList();			
			}
			else if(resultCode == RESULT_CANCELED){
				//failure in activity 2, handling code needed here?
			}
		}
	}
	
	public void populateList(){	
		//uses loop to populate list from array of items, after flushing all previous entries
		m_itemListView.removeAllViews(); //flush all views in LinearLayout
		
		//repopulate LinearLayout
		for(int i = 0; i<array.size(); i++){
			TextView newItemTextView = new TextView(this);  //creates a new textView item 
			newItemTextView.setOnLongClickListener(this);  //sets listener for long press	
			newItemTextView.setText(array.get(i).ItemToString()); //sets text of view to the value of item
			m_itemListView.addView(alternateColors(newItemTextView)); //adds textView item to our list layout after setting background color
		}
	}
	
	public TextView alternateColors(TextView v){
		//alternates colors
		i++;
		if((i%2) == 0){
			v.setBackgroundColor(getResources().getColor(R.color.light));
		}else{
			v.setBackgroundColor(getResources().getColor(R.color.dark));
		}	
		return v;
	}
	
	public boolean onLongClick(View v) {
		TextView tv = (TextView) v;
		String message = tv.getText().toString();
		Intent smsIntent = new Intent(Intent.ACTION_VIEW);
		smsIntent.putExtra("sms_body", "To-Do List Item: " + message);
		smsIntent.putExtra("address", "");
		smsIntent.setType("vnd.android-dir/mms-sms");	
		startActivity(smsIntent);
		return false;
	}
}
